package com.cdshangpai.utils.storage;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.annotation.RequiresApi;

import java.io.File;

public class FileBase {
    protected ContentResolver resolver;
    protected Context mContext;

    public FileBase(Context context)
    {
        this.mContext=context;
    }

    /**
     * android10以上构造读写文件
     * @param path
     * @param fileName
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    protected Uri getFileUrl(String path, String fileName)
    {
        String mimeType;
        String ext=getFileType(fileName);
        if(getFileType(fileName).equals(".mp4"))
        {
            mimeType="video/mp4";
        }else
        {
//            mimeType="application/vnd.android.package-archive";
            mimeType="image/png";
        }
        resolver=mContext.getContentResolver();
        ContentValues values=new ContentValues();
        values.put(MediaStore.Downloads.DISPLAY_NAME,fileName);
        values.put(MediaStore.Downloads.MIME_TYPE,mimeType);
        values.put(MediaStore.Downloads.RELATIVE_PATH,"Download"+ File.separator +path);
        Uri external= MediaStore.Downloads.EXTERNAL_CONTENT_URI;
        Uri uri=resolver.insert(external,values);
        return uri;
    }

    private String getFileType(String fileName)
    {
        Integer pos=fileName.lastIndexOf(".");
        String ext=fileName.substring(pos);
        return ext;
    }
}
